home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Leap Years
- Date: Fri, 01 Mar 96 12:41:35 GMT
- Organization: none
- Message-ID: <825684095snz@genesis.demon.co.uk>
- References: <8BA8405.02C70020E1.uuout@sourcebbs.com> <4h6ara$mu@netnews1.apci.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4h6ara$mu@netnews1.apci.com> wardmw@apci.com "Martin Ward" writes:
-
- >david.mohorn@sourcebbs.com (DAVID MOHORN) typed:
- >
- >>How do you feature out leap years? If its evenly divisible by 400 and
- >>4?
- >
- >The following code will do it:
- >
- >/* Return TRUE if iYear is a leap year, otherwise return FALSE */
- >
- >int IsLeapYear(int iYear)
- >{
- > /* Check for a 4000th year */
- > if ( (iYear % 4000) == 0)
- > return FALSE;
-
- Read the FAQ (and the article recently posted by Steve Summit) - there is
- *no* 4000 term in the Gregorian calculation of leap years.
-
- >
- > /* Check for 400th year */
- > if ( (iYear % 400) == 0)
- > return TRUE;
- >
- > /* Check for century */
- > if ( (iYear % 100) == 0 )
- > return FALSE;
- >
- > /* C
- > if ( (iYear % 4) == 0 )
- > return TRUE;
- >
- > return FALSE;
- >}
-
- This is also not the best way of writing the calculation since it performs
- unnecessary work for most years.
-
- int IsLeapYear(int year)
- {
- if (year % 4 != 0)
- return FALSE;
-
- if (year % 100 != 0)
- return TRUE;
-
- if (year % 400 != 0)
- return FALSE;
-
- return TRUE;
- }
-
- This boils down quite neatly to a single expression as the FAQ shows.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-